from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from .. import models, schemas
from ..crud import post as post_crud
from ..database import get_db
from ..deps import get_current_user
from typing import List

router = APIRouter()

@router.post("/", response_model=schemas.Post)
async def create_post(
    post: schemas.PostCreate,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """发布帖子"""
    return post_crud.create_post(db, post, current_user.id)

@router.get("/", response_model=List[schemas.Post])
async def get_post_list(
    skip: int = 0,
    limit: int = 10,
    db: Session = Depends(get_db)
):
    """获取帖子列表"""
    return post_crud.get_post_list(db, skip=skip, limit=limit)

@router.get("/{post_id}", response_model=schemas.Post)
async def get_post(
    post_id: str,
    db: Session = Depends(get_db)
):
    """获取帖子详情"""
    post = post_crud.get_post(db, post_id)
    if post is None:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return post

@router.put("/{post_id}", response_model=schemas.Post)
async def update_post(
    post_id: str,
    post_update: schemas.PostUpdate,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """更新帖子"""
    return post_crud.update_post(db, post_id, post_update, current_user.id)

@router.delete("/{post_id}")
async def delete_post(
    post_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """删除帖子"""
    return post_crud.delete_post(db, post_id, current_user.id)

@router.post("/{post_id}/comments", response_model=schemas.Comment)
async def create_comment(
    post_id: str,
    comment: schemas.CommentCreate,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """评论帖子"""
    return post_crud.create_comment(
        db=db,
        post_id=post_id,
        comment=comment,
        author_id=current_user.id
    )

@router.get("/{post_id}/comments", response_model=List[schemas.Comment])
async def get_post_comment_list(
    post_id: str,
    skip: int = 0,
    limit: int = 10,
    db: Session = Depends(get_db)
):
    """获取帖子评论"""
    return post_crud.get_post_comment_list(db, post_id, skip=skip, limit=limit)

@router.get("/user/me", response_model=List[schemas.Post])
async def get_my_posts(
    skip: int = 0,
    limit: int = 10,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取当前用户的所有帖子"""
    return post_crud.get_user_posts(db, current_user.id, skip=skip, limit=limit)

@router.get("/user/{user_id}", response_model=List[schemas.Post])
async def get_user_posts(
    user_id: str,
    skip: int = 0,
    limit: int = 10,
    db: Session = Depends(get_db)
):
    """获取指定用户的所有帖子"""
    return post_crud.get_user_posts(db, user_id, skip=skip, limit=limit)

@router.post("/{post_id}/like", response_model=schemas.Post)
async def like_post(
    post_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """给帖子点赞"""
    return post_crud.like_post(db, post_id, current_user.id)

@router.delete("/{post_id}/like", response_model=schemas.Post)
async def unlike_post(
    post_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """取消帖子点赞"""
    return post_crud.unlike_post(db, post_id, current_user.id)

@router.get("/{post_id}/like/check")
async def check_post_liked(
    post_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """检查是否已点赞"""
    is_liked = post_crud.check_post_liked(db, post_id, current_user.id)
    return {"is_liked": is_liked}

@router.post("/{post_id}/comments/{comment_id}/replies", response_model=schemas.Comment)
async def create_comment_reply(
    post_id: str,
    comment_id: str,
    comment: schemas.CommentCreate,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """回复评论"""
    return post_crud.create_comment(
        db=db,
        post_id=post_id,
        comment=comment,
        author_id=current_user.id,
        parent_id=comment_id
    )

@router.get("/comments/{comment_id}/replies", response_model=List[schemas.Comment])
async def get_comment_replies(
    comment_id: str,
    skip: int = 0,
    limit: int = 10,
    db: Session = Depends(get_db)
):
    """获取评论的回复列表"""
    return post_crud.get_comment_replies(db, comment_id, skip=skip, limit=limit)